package com.li.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.eduservice.entity.EduCourse;
import com.li.eduservice.entity.EduTeacher;
import com.li.eduservice.entity.vo.CourseInfoVo;
import com.li.eduservice.entity.vo.CoursePublishVo;
import com.li.eduservice.entity.vo.CourseQuery;
import com.li.eduservice.entity.vo.TeacherQuery;
import com.li.eduservice.service.EduCourseService;
import com.li.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishInfo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishInfo);
    }

    //课程最终发布 就是修该课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表 基本实现

    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }

    //分页查询课程
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable Long current,@PathVariable Long limit){

        Page<EduCourse> page = new Page<>(current,limit);
        //调用实现分页方法
        courseService.page(page,null);
        //总记录数
        long total = page.getTotal();
        //数据list集合
        List<EduCourse> records = page.getRecords();

        HashMap map = new HashMap();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    //多条件查询分页
    @PostMapping("pageCourseCondition/{current}/{limit}")//用@RequestBody  @GetMapper取不到需要@PostMapping提交方式   required = false表示参数可以没有
    public R pageCourseCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CourseQuery courseQuery){

        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current, limit);

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        //调用方法实现条件查询分页
        courseService.page(pageCourse,wrapper);
        //总记录数
        long total = pageCourse.getTotal();
        //数据list集合
        List<EduCourse> records = pageCourse.getRecords();

        HashMap map = new HashMap();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

}

